
//
//  AlbumPreviewVC.m
//  ProjectSource_Demo
//
//  Created by Liu on 16/2/3.
//  Copyright © 2016年 AngryBear. All rights reserved.
//

#import "AlbumPreviewVC.h"
#import <Photos/Photos.h>

@interface AlbumPreviewVC ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *sendButton;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation AlbumPreviewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:self.leftButton];
    [self.view addSubview:self.rightButton];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)backToLastVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightButtonOnClicked:(UIButton *)button
{
    if ([button.imageView.image isEqual:[UIImage imageNamed:@"nav_album_selected"]]) {
        [button setImage:[UIImage imageNamed:@"nav_album_unselected"] forState:UIControlStateNormal];
    }
    else {
        [button setImage:[UIImage imageNamed:@"nav_album_selected"] forState:UIControlStateNormal];
    }
}

- (void)sendButtonOnClicked
{
    self.pageControl.numberOfPages = 3;
}

- (void)albumPreviewShowImages:(NSArray *)images
{
    [self.view addSubview:self.scrollView];
    [self.view sendSubviewToBack:self.scrollView];
    
    for (int i = 0; i < images.count; i++) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        scrollView.backgroundColor = [UIColor blackColor];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.bounces = NO;
        scrollView.maximumZoomScale = 2.0f;
        [self.scrollView addSubview:scrollView];
        scrollView.backgroundColor = [UIColor blueColor];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.backgroundColor = [UIColor whiteColor];
        imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [scrollView addSubview:imageView];
        
        UIImage *image = [images objectAtIndex:i];
        if ([image isKindOfClass:[NSString class]]) {
            
        }
        else if ([image isKindOfClass:[UIImage class]]) {
            imageView.image = image;
            CGSize size = [image relativeScreenWidthSize];
            imageView.frame = CGRectMake(0, 0, size.width, size.height);
            imageView.center = self.scrollView.center;
        }
        else if ([image isKindOfClass:[PHAsset class]]) {
            PHAsset *asset = (PHAsset *)image;
            [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:self.view.bounds.size contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
                if (result) {
                    imageView.image = result;
                    CGSize size = [result relativeScreenWidthSize];
                    imageView.frame = CGRectMake(0, 0, size.width, size.height);
                    imageView.center = self.scrollView.center;
                }
            }];
        }
    }
    
    self.scrollView.contentSize = CGSizeMake(self.view.bounds.size.width*images.count, self.view.bounds.size.height);
    
    if (images.count > 1) {
        self.pageControl.numberOfPages = images.count;
        self.pageControl.currentPage = 0;
        [self.view addSubview:self.pageControl];
        [self.view addSubview:self.sendButton];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    self.pageControl.currentPage = index;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    UIView *view = [self viewForZoomingInScrollView:scrollView];
    CGSize boundsSize = scrollView.bounds.size;
    CGRect imgFrame = view.frame;
    CGSize contentSize = scrollView.contentSize;
    
    CGPoint centerPoint = CGPointMake(contentSize.width/2, contentSize.height/2);
    
    // center horizontally
    if (imgFrame.size.width <= boundsSize.width)
    {
        centerPoint.x = boundsSize.width/2;
    }
    
    // center vertically
    if (imgFrame.size.height <= boundsSize.height)
    {
        centerPoint.y = boundsSize.height/2;
    }
    
    view.center = centerPoint;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    UIView *view = [scrollView.subviews lastObject];
    return view;
}

#pragma mark - Property Methods
- (UIButton *)rightButton
{
    if (!_rightButton) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 44 - 10, 20, 44, 44)];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(rightButtonOnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"nav_album_unselected"] forState:UIControlStateNormal];
        _rightButton = button;
    }
    return _rightButton;
}

- (UIButton *)leftButton
{
    if (!_leftButton) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 44, 44)];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(backToLastVC) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageNamed:@"nav_album_back"] forState:UIControlStateNormal];
        _leftButton = button;
    }
    return _leftButton;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        scrollView.backgroundColor = [UIColor blackColor];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.delegate = self;
        scrollView.pagingEnabled = YES;
        scrollView.bounces = NO;
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        _scrollView = scrollView;
    }
    return _scrollView;
}

- (UIButton *)sendButton
{
    if (!_sendButton) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 80 - 12, self.view.bounds.size.height - 10 - 36, 80, 36)];
        button.backgroundColor = [UIColor clearColor];
        [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(sendButtonOnClicked) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"发送" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _sendButton = button;
    }
    return _sendButton;
}

- (UIPageControl *)pageControl
{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 40, self.view.bounds.size.width, 20)];
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.backgroundColor = [UIColor clearColor];
    }
    return _pageControl;
}

@end
